package com.qianduo.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class AbstractAction extends DispatchAction{
	protected final String className					= this.getClass().getName();
	protected final static String FORWARD_DEFAULT_KEY 	= "default";
	protected final static String FORWARD_FAILURE_KEY 	= "failure";
	protected final static String SAVE_OR_UPDATE		= "save_or_update";
	protected final static String LIST					= "list";	
	protected static ApplicationContext ctx 			= null;
	
	public final ActionForward unspecified(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception {
		ActionForward forward = mapping.findForward(FORWARD_DEFAULT_KEY);		
	    if (forward == null) {
	    	forward = mapping.findForward(FORWARD_FAILURE_KEY);
	    }
	    if (forward != null) {
	    	return forward;
	    }
	    return super.unspecified(mapping, form, request, response);
	}
	
	 /**
     * Convenience method to bind objects in Actions
     * @param name
     * @return
     */
    public Object getBean(String name) {
        if (null == ctx) {
            ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
        }
        return ctx.getBean(name);
    }
}
